
// Copyright (C) 2009-2011 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;

using Derm.Render;

namespace Derm.Render.Mesh
{
	/// <summary>
	/// Mesh manager. 
	/// </summary>
	/// <remarks>
	/// <para>
	/// 
	/// </para>
	/// </remarks>
	public class MeshCollection
	{
		#region Template Mesh Management
		
		/// <summary>
		/// Item which keep 
		/// </summary>
		private struct MeshTemplate
		{
			/// <summary>
			/// MeshTemplate constructor. 
			/// </summary>
			/// <param name="path">
			/// A <see cref="System.String"/>
			/// </param>
			public MeshTemplate(string path)
			{
				mTemplate = null;
				mPath = path;
			}
			
			/// <summary>
			/// Template mesh. 
			/// </summary>
			public Mesh mTemplate;
			/// <summary>
			/// Path where to load mesh resource. 
			/// </summary>
			public string mPath;
		}
		
		/// <summary>
		/// Cache a commonly used mesh.
		/// </summary>
		/// <param name="name">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="path">
		/// A <see cref="System.String"/>
		/// </param>
		public static void CacheTemplateMesh(string name, string path)
		{
			MeshTemplate mTemplate = new MeshTemplate(path);
			
			// Test the existence of the mesh file
			
			// Check whether the mesh template name is not registered
			if (sMeshCache.ContainsKey(name) == true)
				throw new Exception("Template mesh "+name+" already cached.");
			// Append the mesh to cache, without loading
			sMeshCache.Add(name, mTemplate);
		}

		/// <summary>
		/// Copy a cached template mesh.
		/// </summary>
		/// <param name="name">
		/// A <see cref="System.String"/>
		/// </param>
		/// <remarks>
		/// Mesh data is shared with every template mesh instance.
		/// </remarks>
		public static Mesh CopyTemplateMesh(string name)
		{
			MeshTemplate mTemplate;
			Mesh mesh = null;
			
			// Get template mesh reference
			if (sMeshCache.TryGetValue(name, out mTemplate) == true) {
				// Never cached?
				if (mTemplate.mTemplate == null) {
					// Load template mesh
					mTemplate.mTemplate = MeshCodec.Load(mTemplate.mPath);
					// Store template in the cache
					sMeshCache[name] = mTemplate;
				}

				// Clone caches mesh
				mesh = new Mesh(mTemplate.mTemplate);
			}   
			
			return (mesh);
		}
		
		/// <summary>
		/// Template mesh cache.
		/// </summary>
		private static Dictionary<string, MeshTemplate> sMeshCache = new Dictionary<string, MeshTemplate>();
		
		#endregion
	}
}
